package com.rstrulak.managedBeans.controllers;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.ProjectDao;
import com.rstrulak.dao.TaskDao;
import com.rstrulak.dao.UserProjectDao;
import com.rstrulak.model.Project;
import com.rstrulak.model.UserProject;
import com.rstrulak.utils.Constants;
import com.rstrulak.utils.JSFUtils;

@Component("projectListBean")
@Scope("request")
public class ProjectListBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1375446788166264004L;

	@Autowired
	private ProjectDao projectDao;

	@Autowired
	private TaskDao taskDao;

	@Autowired
	private UserProjectDao userProjectDao;

	private List<Project> projectList = null;

	private List<UserProject> userProjectList = null;

	@SuppressWarnings("unused")
	private Long loggedUserId;

	@PostConstruct
	public void init() {
		JSFUtils.reloadFlash();
		getProjectListFromDB();
	}

	public Long getLoggedUserId() {
		return JSFUtils.getLoggedUser().getId();
	}

	public void setLoggedUserId(Long loggedUserId) {
		this.loggedUserId = loggedUserId;
	}

	private void getProjectListFromDB() {
		userProjectList = userProjectDao.getProjectListByUser(JSFUtils.getLoggedUser());
	}

	public List<Project> getProjectList() {
		return projectList;
	}

	public void setProjectList(List<Project> projectList) {
		this.projectList = projectList;
	}

	public List<UserProject> getUserProjectList() {
		return userProjectList;
	}

	public void setUserProjectList(List<UserProject> userProjectList) {
		this.userProjectList = userProjectList;
	}

	public void showProjectDetails(Long projectId) throws ServletException, IOException {
		JSFUtils.flashScope().put(Constants.PROJECT_ID, projectId);
		JSFUtils.redirect("/protected/projectDetails.xhtml");
	}

	public void goToCreateProjectSite() throws ServletException, IOException {
		JSFUtils.redirect("/protected/newProject.xhtml");
	}

	public void goToCreateContextSite() throws ServletException, IOException {
		JSFUtils.redirect("/protected/newContext.xhtml");
	}

	public void deleteProject(Project project) throws ServletException, IOException {

		// Boolean deleteProject = (Boolean)
		// JSFUtils.flashScope().get(Contsants.DELETE_PROJECT);
		// JSFUtils.flashScope().put(Contsants.PROJECT_ID_TO_DELETE, projectId);
		// if (deleteProject != null & deleteProject) {
		taskDao.deleteAllTaskFromProject(project);
		projectDao.delete(project);
		getProjectListFromDB();
		JSFUtils.redirect("/protected/project.xhtml");
		// }
	}

	public void deleteProject() throws ServletException, IOException {
		// Long projectId =
		// Long.getLong(JSFUtils.getExternalContext().getSessionMap().get("projectItem"));
		// if(projectId !=null){
		// Project project = projectDao.getById(projectId);
		// taskDao.deleteAllTaskFromProject(project);
		// projectDao.delete(projectId);
		// getProjectListFromDB();
		// JSFUtils.redirect("/faces/project.xhtml");
		// }
	}

	public void initializeFlag() {
		// JSFUtils.flashScope().put(Contsants.DELETE_PROJECT, Boolean.TRUE);
		// JSFUtils.getExternalContext().getRequestParameterMap().get("projectItem");
	}

}
